﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using SquareTasks.Models;
using SquareTasks.Views.Home;

namespace SquareTasks.Controllers
{
    
    public class HomeController : Controller
    {
        //
        // GET: /Home/

        SquareTasksContext db = new SquareTasksContext();
        public ActionResult Index()
        {
            return View();
        }


        public ActionResult accueil()
        {
            return PartialView();
        }

        public ActionResult corbeille()
        {
            return PartialView();
        }

        [HttpPost]
        public ActionResult corbeille(TaskViewModel laTacheASupprimer)
        {
            var laTache = parcoursTaches(laTacheASupprimer.id);
            laTache.EtatTache = Etat.Corbeille;
            laTache.favori = false;

            db.Entry(laTache).State = System.Data.EntityState.Modified;
            
            db.SaveChanges();
            
            return datas();
        }

        public ActionResult account()
        {
            return PartialView();
        }

        [HttpGet]
        public ActionResult add()
        {
            return PartialView();
        }
        [HttpPost]
        public ActionResult add(addViewModel newTacheVM)
        {

            Tasks newTache = new Tasks();

            newTache.Id = newTacheVM.id;
            newTache.Description = newTacheVM.description;            
            //DateTime dt = new DateTime(0001, 01, 01, 00, 00, 00, 1);
            if (newTacheVM.date.Year.Equals(1))
            {
                newTache.dateCreation = DateTime.Now;
            }
            else
            {
                newTache.dateCreation = newTacheVM.date;
            }
            newTache.lieu = newTacheVM.lieu;
            newTache.Objet = newTacheVM.objet;
            if (newTacheVM.priorite == 0)
            {
                newTache.priorite = 75;
            }
            else
            {
                newTache.priorite = newTacheVM.priorite;
            }            
            /*** à optimiser ***/
            if (newTacheVM.categ != null)
            {
                var res = from cat in db.Categories where cat.NomCategorie == newTacheVM.categ select cat;
                newTache.Categorie = res.First();
            }
            else
            {
                var res = from cat in db.Categories where cat.NomCategorie == "Indéfini" select cat;
                if (res.Count() != 0)
                {
                    newTache.Categorie = res.First();
                }
                else
                {
                    Category newCateg = new Category();
                    newCateg.NomCategorie = "Indéfini";
                    newTache.Categorie = newCateg;
                }
                
            }
            /*** à optimiser ***/

            db.Tasks.Add(newTache);

            db.SaveChanges();

            return datas();
        }
        [HttpPost]
        public ActionResult edit(addViewModel newTacheVM)
        {
            
            Tasks editTache = db.Tasks.Find(newTacheVM.id);

            editTache.Description = newTacheVM.description;
            if (newTacheVM.date.Year.Equals(1))
            {
                editTache.dateCreation = DateTime.Now;
            }
            else
            {
                editTache.dateCreation = newTacheVM.date;
            }
            editTache.lieu = newTacheVM.lieu;
            editTache.Objet = newTacheVM.objet;
            editTache.priorite = newTacheVM.priorite;

            /*** à optimiser ***/
            if (newTacheVM.categ != null)
            {
                var res = from cat in db.Categories where cat.NomCategorie == newTacheVM.categ select cat;
                editTache.Categorie = res.First();
            }
            /*** à optimiser ***/

            db.SaveChanges();

            return null;
        }

        [HttpPost]
        public ActionResult quickAdd(addViewModel newTacheVM)
        {
            Tasks newTache = new Tasks();

            newTache.Id = newTacheVM.id;
            newTache.Description = newTacheVM.description;
            newTache.lieu = newTacheVM.lieu;
            newTache.dateCreation = DateTime.Now;
            newTache.Objet = newTacheVM.objet;
            newTache.priorite = newTacheVM.priorite;

            newTache.Categorie = db.Categories.Find(3);

            db.Tasks.Add(newTache);

            db.SaveChanges();

            return datas();
        }


        public ActionResult favoris()
        {
            return PartialView();
        }

        [HttpPost]
        public ActionResult favoris(TaskViewModel laTacheFavorite)
        {
            var laTache = parcoursTaches(laTacheFavorite.id);
            laTache.favori = true;

            db.Entry(laTache).State = System.Data.EntityState.Modified;

            db.SaveChanges();

            //var json = this.datas();

            return datas();
                //json;
        }

        public ActionResult historique()
        {
            return PartialView();
        }

        [HttpPost]
        public ActionResult historique(TaskViewModel laTacheTerminee)
        {
            var laTache = parcoursTaches(laTacheTerminee.id);
            laTache.EtatTache = Etat.Historique;
            laTache.favori = false;

            db.Entry(laTache).State = System.Data.EntityState.Modified;

            db.SaveChanges();

            return datas();
        }

        public ActionResult search()
        {
            return PartialView();
        }

        public JsonResult  datas()
        {
            List<TaskViewModel> listVM = new List<TaskViewModel>();
            if (db.Tasks.Count() == 0)
                return Json(listVM.ToList(), JsonRequestBehavior.AllowGet);

            foreach (var o in db.Tasks)
            {
                listVM.Add(mappingModelToJSON(o));
            }

            var json = Json(listVM.ToList(), JsonRequestBehavior.AllowGet);
            return json;
        }

        public TaskViewModel mappingModelToJSON(Tasks tache)
        {
            TaskViewModel vm = new TaskViewModel();
            vm.id = tache.Id;
            vm.objet = tache.Objet;
            vm.description = tache.Description;
            vm.date = System.Convert.ToString(tache.dateCreation.Date);

            

            vm.categ = tache.Categorie.NomCategorie;

            vm.lieu = tache.lieu;

            if(tache.EtatTache.Equals("EnCours"))
            {
                vm.corbeille = 0;
                vm.historique = 0;
            }
            else if(tache.EtatTache.Equals("Historique"))
            {
                vm.corbeille = 0;
                vm.historique = 1;
            }
            else if(tache.EtatTache.Equals("Corbeille"))
            {
                vm.corbeille = 1;
                vm.historique = 0;
            }
            if (tache.favori)
            {
                vm.favori = 1;
            }
            else 
            {
                vm.favori = 0;
            }
            vm.priorite = tache.priorite;
            if (vm.priorite == 75)
            {
                vm.couleur = "prio3";
            }
            else if (vm.priorite == 150)
            {
                vm.couleur = "prio2";
            }
            else if (vm.priorite == 300)
            {
                vm.couleur = "prio1";
            }

            return vm;
        }

        public Tasks parcoursTaches(int idTache)
        {
            var q = from i in db.Tasks
                    where i.Id == idTache
                    select i;

            return q.FirstOrDefault();
 
            //db.tasks.where(t
            //foreach (var t in db.Tasks)
            //{
            //    if (t.Id == idTache)
            //    {
            //        return t;
            //    }
            //}

            //return null;
        }
    }
}
